﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ConfigurationTool
{
    public partial class Form1 : Form
    {
        Configuration configuration;
        Dictionary<String, TextBox> txtBoxes;
        Dictionary<String, ComboBox> xboxCmbBoxes;
        Dictionary<String, ComboBox> wiiCmbBoxes;
        Dictionary<String, ComboBox> otherCmbBoxes;
        Dictionary<String, CheckBox> otherChkBoxes;
        List<String> tabs;
        List<String> commands;
        List<String> xboxButtons;
        List<String> wiiButtons;
        List<String> otherSettings;
        List<String> graphics;
        List<String> difficulty;

        public Form1()
        {
            InitializeComponent();

            configuration = new Configuration();

            txtBoxes = new Dictionary<String, TextBox>();
            xboxCmbBoxes = new Dictionary<String, ComboBox>();
            wiiCmbBoxes = new Dictionary<String, ComboBox>();
            otherCmbBoxes = new Dictionary<String, ComboBox>();
            otherChkBoxes = new Dictionary<String, CheckBox>();

            tabs = new List<String>();
            tabs.Add("Keyboard");
            tabs.Add("Gamepad");
            tabs.Add("Wiimote");
            tabs.Add("Other Settings");

            commands = new List<String>();
            commands.Add("Score");
            commands.Add("Pouse");
            commands.Add("Music On/Off");
            commands.Add("Sound On/Off");
            commands.Add("Help");
            commands.Add("Screenshot");
            commands.Add("FPS");
            commands.Add("Up");
            commands.Add("Down");
            commands.Add("Right");
            commands.Add("Left");
            commands.Add("Jump");
            commands.Add("Shoot");
            commands.Add("Club");

            graphics = new List<String>();
            graphics.Add("640x480");
            graphics.Add("800x600");
            graphics.Add("1024x768");
            graphics.Add("1280x1024");
            graphics.Add("1440x900");

            difficulty = new List<String>();
            difficulty.Add("Easy");
            difficulty.Add("Medium");
            difficulty.Add("Hard");

            xboxButtons = new List<String>();
            xboxButtons.Add("Start");
            xboxButtons.Add("XButton");
            xboxButtons.Add("Back");
            xboxButtons.Add("RShoulder");
            xboxButtons.Add("RTrigger");
            xboxButtons.Add("LShoulder");
            xboxButtons.Add("LTrigger");
            xboxButtons.Add("X");
            xboxButtons.Add("Y");
            xboxButtons.Add("A");
            xboxButtons.Add("B");
            xboxButtons.Add("Left");
            xboxButtons.Add("Up");
            xboxButtons.Add("Bottom");
            xboxButtons.Add("Right");

            wiiButtons = new List<String>();
            wiiButtons.Add("Bottom");
            wiiButtons.Add("1");
            wiiButtons.Add("2");
            wiiButtons.Add("A");
            wiiButtons.Add("Home");
            wiiButtons.Add("-");
            wiiButtons.Add("+");
            wiiButtons.Add("Left");
            wiiButtons.Add("Up");
            wiiButtons.Add("B");
            wiiButtons.Add("Right");

            otherSettings = new List<String>();
            otherSettings.Add("Difficulty");
            otherSettings.Add("VSync");
            otherSettings.Add("Graphics");

            initializeCollection();

            initializeComboBoxes();

            try
            {
                this.configuration.read();
            }
            catch (Exception e)
            {
                this.configuration.writeDefault();

                this.configuration.read();
            }

            SetControls();
        }

        private void setTextBoxesKeyboard(TabPage page)
        {
            int i = 0;

            foreach (Control control in page.Controls)
            {
                try
                {
                    TableLayoutPanel tbl = (TableLayoutPanel)control;

                    foreach (Control txt in tbl.Controls)
                    {
                        try
                        {
                            TextBox tmp = (TextBox)txt;
                            txtBoxes[commands[i]] = tmp;
                            ++i;
                        }
                        catch (Exception e)
                        { }
                    }
                }
                catch (Exception e)
                { }
            }
        }
        private void setComboBoxesXbox(TabPage page)
        {
            int i = 0;

            foreach (Control control in page.Controls)
            {
                try
                {
                    ComboBox tmp = (ComboBox)control;
                    xboxCmbBoxes[xboxButtons[i]] = tmp;
                    ++i;
                }
                catch (Exception e)
                { }
            }
        }
        private void setComboBoxesWii(TabPage page)
        {
            int i = 0;

            foreach (Control control in page.Controls)
            {
                try
                {
                    ComboBox tmp = (ComboBox)control;
                    wiiCmbBoxes[wiiButtons[i]] = tmp;
                    ++i;
                }
                catch (Exception e)
                { }
            }
        }
        private void setComboBoxesOther(TabPage page)
        {
            int i = 0;

            foreach (Control gpb in page.Controls)
            {
                try
                {
                    GroupBox gTmp = (GroupBox)gpb;

                    foreach (Control control in gpb.Controls)
                    {
                        try
                        {
                            ComboBox tmp = (ComboBox)control;
                            otherCmbBoxes[otherSettings[i]] = tmp;
                            ++i;
                        }
                        catch (Exception e)
                        {
                            i = 2;
                        }
                    }
                }
                catch (Exception e)
                { }
            }
        }
        private void setCheckBoxesOther(TabPage page)
        {
            foreach (Control gpb in page.Controls)
            {
                try
                {
                    GroupBox gTmp = (GroupBox)gpb;

                    foreach (Control control in gpb.Controls)
                    {
                        try
                        {
                            CheckBox tmp = (CheckBox)control;
                            otherChkBoxes[otherSettings[1]] = tmp;
                        }
                        catch (Exception e)
                        { }
                    }
                }
                catch (Exception e)
                { }
            }
        }

        private void initializeCollection()
        {
            try
            {
                foreach (TabControl tab in this.Controls)
                {
                    try
                    {
                        foreach (TabPage page in tab.TabPages)
                        {
                            try
                            {
                                if (page.Text.Equals(tabs[0]))
                                {
                                    setTextBoxesKeyboard(page);
                                }
                                else if (page.Text.Equals(tabs[1]))
                                {
                                    setComboBoxesXbox(page);
                                }
                                else if (page.Text.Equals(tabs[2]))
                                {
                                    setComboBoxesWii(page);
                                }
                                else if (page.Text.Equals(tabs[3]))
                                {
                                    setComboBoxesOther(page);
                                    setCheckBoxesOther(page);
                                }
                            }
                            catch (Exception e)
                            { }
                        }
                    }
                    catch (Exception e)
                    { }
                }
            }
            catch (Exception e)
            { }
        }

        private void initializeComboBoxes()
        {
            for (int i = 0; i < xboxButtons.Count; ++i)
            {
                xboxCmbBoxes[xboxButtons[i]].Items.AddRange(commands.ToArray());
            }

            for (int i = 0; i < wiiButtons.Count; ++i)
            {
                wiiCmbBoxes[wiiButtons[i]].Items.AddRange(commands.ToArray());
            }

            otherCmbBoxes[otherSettings[0]].Items.AddRange(difficulty.ToArray());
            otherCmbBoxes[otherSettings[2]].Items.AddRange(graphics.ToArray());
        }

        private void SetControls()
        {
            foreach (var secKey in this.configuration.getSections().Keys)
            {
                Dictionary<String, Control> d = new Dictionary<String,Control>();
                foreach (var item in this.configuration.getSections()[secKey])
                {
                    d.Add(item.Key, item.Value);
                }
                
                foreach (String k in d.Keys)
                {
                    if (secKey.Equals(tabs[0]))
                    {
                        txtBoxes[k].Text = d[k].Text;
                    }
                    else if (secKey.Equals(tabs[1]))
                    {
                        xboxCmbBoxes[k].Text = d[k].Text;
                    }
                    else if (secKey.Equals(tabs[2]))
                    {
                        wiiCmbBoxes[k].Text = d[k].Text;
                    }
                    else if (secKey.Equals(tabs[3]))
                    {
                        if (!k.Equals("VSync"))
                            otherCmbBoxes[k].Text = d[k].Text;
                        else
                        {
                            if (d[k].Text.Equals("True"))
                                otherChkBoxes[k].Checked = true;
                            else
                                otherChkBoxes[k].Checked = false;
                        }
                    }
                }

            }
        }

        private void txtLeft_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtLeft);
            f2.ShowDialog(this);

            if (txtLeft.Text != "")
            {
                configuration.bind(tabs[0], commands[10], txtLeft);
            }
        }

        private void txtRight_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtRight);
            f2.ShowDialog(this);

            if (txtRight.Text != "")
            {
                configuration.bind(tabs[0], commands[9], txtRight);
            }
        }

        private void txtDown_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtDown);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[8], txtDown);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // write the configuration file
            this.configuration.write();
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            // apply the default configuration
            this.configuration.writeDefault();
            this.configuration.read();

            SetControls();
        }

        private void txtUp_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtUp);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[7], txtUp);
            }
        }

        private void txtJump_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtJump);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[11], txtJump);
            }
        }

        private void txtShoot_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtShoot);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[12], txtShoot);
            }
        }

        private void txtClub_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtClub);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[13], txtClub);
            }
        }

        private void txtScore_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtScore);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[0], txtScore);
            }
        }

        private void txtPause_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtPause);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[1], txtPause);
            }
        }

        private void txtMusic_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtMusic);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[2], txtMusic);
            }
        }

        private void txtSound_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtSound);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[3], txtSound);
            }
        }

        private void txtHelp_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtHelp);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[4], txtHelp);
            }
        }

        private void txtScreenshot_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtScreenshot);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[5], txtScreenshot);
            }
        }

        private void txtFPS_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(txtFPS);
            f2.ShowDialog(this);

            if (txtDown.Text != "")
            {
                configuration.bind(tabs[0], commands[6], txtFPS);
            }
        }

        private void cmbLTrigger_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[6], cmbLTrigger);
        }

        private void cmbRTrigger_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[4], cmbRTrigger);
        }

        private void cmbLShoulder_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[5], cmbLShoulder);
        }

        private void cmbRShoulder_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[3], cmbRShoulder);
        }

        private void cmbBack_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[2], cmbBack);
        }

        private void cmbXButton_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[1], cmbXButton);
        }

        private void cmbStart_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[0], cmbStart);
        }

        private void cmbUp_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[12], cmbUp);
        }

        private void cmbLeft_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[11], cmbLeft);
        }

        private void cmbRight_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[14], cmbRight);
        }

        private void cmbBottom_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[13], cmbBottom);
        }

        private void cmbY_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[8], cmbY);
        }

        private void cmbX_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[7], cmbX);
        }

        private void cmbB_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[10], cmbB);
        }

        private void cmbA_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[1], xboxButtons[9], cmbA);
        }

        private void cmbUpWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[8], cmbUpWii);
        }

        private void cmbBottomWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[0], cmbBottomWii);
        }

        private void cmbRightWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[10], cmbRightWii);
        }

        private void cmbLeftWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[7], cmbLeftWii);
        }

        private void cmbBWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[9], cmbBWii);
        }

        private void cmbAWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[3], cmbAWii);
        }

        private void cmb1Wii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[1], cmb1Wii);
        }

        private void cmb2Wii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[2], cmb2Wii);
        }

        private void cmbPlusWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[6], cmbPlusWii);
        }

        private void cmbMinusWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[5], cmbMinusWii);
        }

        private void cmbHomeWii_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[2], wiiButtons[4], cmbHomeWii);
        }

        private void chkVSync_CheckedChanged(object sender, EventArgs e)
        {
            TextBox text = new TextBox();

            if (chkVSync.Checked)
                text.Text = "True";
            else
                text.Text = "False";

            configuration.bind(tabs[3], otherSettings[1], text);
        }

        private void cmbGraphics_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[3], otherSettings[2], cmbGraphics);
        }

        private void cmbDifficulty_SelectedIndexChanged(object sender, EventArgs e)
        {
            configuration.bind(tabs[3], otherSettings[0], cmbDifficulty);
        }

        private void sAveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "ini files (*.ini)|*.ini";

            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                this.configuration.write(saveFileDialog1.FileName);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "ini files (*.ini)|*.ini";

            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                this.configuration.read(openFileDialog1.FileName);
                SetControls();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
